//
//  CategoryViewController.swift
//  FM
//
//  Created by x on 2017/9/5.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

class CategoryViewController: UIViewController {
    
    /// 导航栏标题
    var naviTitle:String?
    
    /// 分类id
    var categoryID:String?
    
    var index:Int = 0
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    /// 初始化UI
    private func setupUI()-> () {
        
        //1.设置界面属性
        view.backgroundColor = UIColor.white
        navigationItem.title = naviTitle
        navigationItem.leftBarButtonItem = UIBarButtonItem.creatBarButtonItem(imageName: "back", target: self, action: #selector(backClick))
        
        //2.添加表格
        view.addSubview(categoryTableView)
        
        //3.加载数据
        NetWorTools.shareNetWorkTools().loadCategoryData(categoryID: categoryID!, offset: 0, successCallBack: { (data) in
            self.radioStations = data
            self.categoryTableView.reloadData()
        }) { (error) in
            print("loadCategoryData======\(error)")
        }
    }
    
    /// 返回按钮
    func backClick() -> Void {
        navigationController?.popViewController(animated: true)
    }
    
    /// 刷新更多数据
    func loadMoreData() -> Void {
        index += 1
        let num  = index * 20
        NetWorTools.shareNetWorkTools().loadCategoryData(categoryID: categoryID!, offset: num, successCallBack: { (data) in
            self.radioStations += data
            self.categoryTableView.reloadData()
            //刷新得到更多数据之后,要停止刷新
            self.categoryTableView.mj_footer.endRefreshing()
        }) { (error) in
            print("loadCategoryData======\(error)")
        }
    }
    //MARK: -- 懒加载
    
    private lazy var categoryTableView:UITableView = {
        let tableView = UITableView()
        tableView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height-64)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        //注册cell
        tableView.register(CategoryTableViewCell.self, forCellReuseIdentifier: categoryCellIdentifier)
        
        tableView.mj_footer = MJRefreshBackNormalFooter(refreshingTarget: self, refreshingAction: #selector(loadMoreData))
        return tableView
    }()
    
    /// 表格的数据源
    fileprivate lazy var radioStations:[HomeFM] = [HomeFM]()
    
    /// 缓存cell的行高
    fileprivate lazy var cellHeightCaches:[String:CGFloat] = [String:CGFloat]()
}

/// cell的标示
private let categoryCellIdentifier = "categoryCellIdentifier"


extension CategoryViewController:UITableViewDataSource,UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return radioStations.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: categoryCellIdentifier) as! CategoryTableViewCell
        cell.model = radioStations[indexPath.row]
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        AnimationTools.addAnimationToKeyWindow(animationType: "rippleEffect")
        
        //2.跳转VC
        let playerController = HomePlayerViewController(data: radioStations, index: indexPath.row)
        navigationController?.pushViewController(playerController, animated: true)
    }
    
    //计算cell的高度
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        let model = radioStations[indexPath.row]
        
        //1.首先冲内存缓存中取出cell的高度
        if (cellHeightCaches[model.id!] != nil) {
            return cellHeightCaches[model.id!]!
        }
        
        //2.如果内存没有,则计算cell的高度
        let cell = tableView.dequeueReusableCell(withIdentifier: categoryCellIdentifier) as! CategoryTableViewCell
        let height = cell.rowHeight(model: model)
        
        //3.把计算的高度添加到内存缓存中
        cellHeightCaches[model.id!] = height
        
        return height
    }
}










